package servlet;

import dao.BookDao;
import dao.IDAO;
import vo.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "AddBookServlet")
public class AddBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        String title=request.getParameter("Book_name");
        String id=request.getParameter("Book_number");
        String author=request.getParameter("Author");
        String press=request.getParameter("Publisher");
        float pric= Float.parseFloat(request.getParameter("Price"));
        Book book=new Book();
        book.setId(id);
        book.setTitle(title);
        book.setAuthor(author);
        book.setPress(press);
        book.setPric(pric);
        IDAO dao=new BookDao();
        boolean i=dao.insert(book);
        if (i){
            request.getRequestDispatcher("selectbookservlet").forward(request,response);
        }else {
            PrintWriter pw=response.getWriter();
            pw.print("添加失败");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
